package com.doublethread.easymart.common.context;

/**
 * 用户上下文工具类
 */
public class UserContext {

    private static final ThreadLocal<Long> USER_ID = new ThreadLocal<>();
    private static final ThreadLocal<String> USERNAME = new ThreadLocal<>();

    /**
     * 设置当前用户信息
     */
    public static void setCurrentUser(Long userId, String username) {
        USER_ID.set(userId);
        USERNAME.set(username);
    }

    /**
     * 获取当前用户ID
     */
    public static Long getCurrentUserId() {
        return USER_ID.get();
    }

    /**
     * 获取当前用户名
     */
    public static String getCurrentUsername() {
        return USERNAME.get();
    }

    /**
     * 判断是否为管理员
     */
    public static boolean isAdmin() {
        String username = getCurrentUsername();
        return "admin".equals(username);
    }

    /**
     * 清除当前用户信息
     */
    public static void clear() {
        USER_ID.remove();
        USERNAME.remove();
    }
}
